package com.gsk.europepharma.salesReport.struts.actions;

import java.security.Principal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;

public class ListeMixAdmin extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Requete 
		tbSpBoSRSectoDAO sectoDao = new tbSpBoSRSectoDAO();
		Session session = sectoDao.getSession();
		Query req = session.getNamedQuery(tbSpBoSRSectoDAO.QUERY_QUERY_LISTE_ADM_PDT_ADMIN);
		ArrayList result = new ArrayList(req.list());
		Iterator iter = result.iterator();
		
		// Map [ BU ; Map [ Reseau ; List < Produit > ] ]
		TreeMap<String, TreeMap<String, List<Object>>> maps = new TreeMap<String, TreeMap<String, List<Object>>>();
		
		while (iter.hasNext()) {
			Object[] tmpBU = (Object[]) iter.next();
			if (maps.get(tmpBU[0].toString()) == null) {
				maps.put(tmpBU[0].toString(), new TreeMap<String, List<Object>>());
			} 
			if (maps.get(tmpBU[0].toString()).get(tmpBU[2].toString()) == null) {
				maps.get(tmpBU[0].toString()).put(tmpBU[2].toString(), new ArrayList<Object>());
			}
			maps.get(tmpBU[0].toString()).get(tmpBU[2].toString()).add(tmpBU);
		}
		
		JSONArray jsonArrayBU = new JSONArray();
		
		int i = 0;
		
		// Construction du tableau JSON pour l arbre des MIX 
		
		for (Map.Entry<String, TreeMap<String, List<Object>>> bu : maps.entrySet()) {
			
			JSONObject jsonObjectBU = new JSONObject();
			JSONObject dataBU = new JSONObject(); 
			JSONObject code = new JSONObject(); 
			code.put("id", bu.getKey().toString() + i);
			i++;
			dataBU.put("attr", code);
			dataBU.put("title", bu.getKey().toString());
			//dataBU.put("icon", "img/cercle-vert.png");
			jsonObjectBU.put("data", dataBU);
			
			JSONArray jsonArrayRes = new JSONArray();
			
			for (Map.Entry<String, List<Object>> res : bu.getValue().entrySet()) {
				
				JSONObject jsonObjectRes = new JSONObject();
				JSONObject dataRes = new JSONObject(); 
				code = new JSONObject(); 
				code.put("id", res.getKey().toString() + i);
				i++;
				dataRes.put("attr", code);
				dataRes.put("title", res.getKey().toString());
				//dataRes.put("icon", "");
				jsonObjectRes.put("data", dataRes);
				
				JSONArray jsonArrayProd = new JSONArray();
				
				for (Object prod : res.getValue()) {
					
					Object[] tmpProd = (Object[]) prod;
					JSONObject jsonObjectProd = new JSONObject();
					JSONObject dataProd = new JSONObject(); 
					code = new JSONObject(); 
					code.put("id", tmpProd[1] + "-" + tmpProd[3]);
					dataProd.put("attr", code);
					dataProd.put("title", tmpProd[4]);
					//dataProd.put("icon", "");
					jsonObjectProd.put("data", dataProd);
					
					jsonArrayProd.add(jsonObjectProd);
					
				}
				
				jsonObjectRes.put("children", jsonArrayProd);
				
				jsonArrayRes.add(jsonObjectRes);
				
			}
			
			jsonObjectBU.put("children", jsonArrayRes);
			
			jsonArrayBU.add(jsonObjectBU);
			
		}
		
		// Utilisateur 
		Principal user = request.getUserPrincipal();
		
		HashMap hm = new HashMap();
		hm.put("data", jsonArrayBU);
		hm.put("login", user.getName());
		
		JSONObject json = JSONObject.fromObject(hm);
		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();
				
		return null;
	}
}
